//
//  NSString+YODUtil.m
//  YOD
//
//  Created by peter on 4/24/15.
//  Copyright (c) 2015 siloon. All rights reserved.
//

#import "NSString+YODUtil.h"
#define REGEX_PATTERN @"[a-zA-Z]"
#define REGEX_PATTERN_WHITESPACE @"[a-zA-Z\\s]"

@implementation NSString(YODUtil)

- (BOOL)contains:(NSString *)str caseSensitive:(BOOL)caseSensitive{
    if (caseSensitive) {
        return [self rangeOfString:str].location != NSNotFound;
    }
    return [self rangeOfString:str options:NSCaseInsensitiveSearch].location != NSNotFound;
}

- (NSString*)trim{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (NSDate*)convertToNSDate:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [formatter setDateFormat:format];
    NSDate *date = [formatter dateFromString:self];
    return date;
}

#pragma mark - 验证
- (BOOL)checkEmail{
    NSError *error             = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[+\\w\\.\\-']+@[a-zA-Z0-9-]+(\\.[a-zA-Z]{2,})+$" options:NSRegularExpressionCaseInsensitive error:&error];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    return numberOfMatches == 1;
}

- (BOOL)checkEmpty{
    if (nil == self)
        return NO;
    
    if (![self isEqualToString: @""]) {
        return YES;
    }
    
    return NO;
}

- (BOOL)checkAlphabeticAllowWhitespace:(BOOL)allowWhitespace{
    if (nil == self || [self isEqualToString: @""])
        return NO;
    
    NSString *pattern = REGEX_PATTERN;
    
    if (allowWhitespace)
    {
        pattern = REGEX_PATTERN_WHITESPACE;
    }
    
    NSError *error             = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    return numberOfMatches == self.length;
}

- (BOOL)checkWithRange:(NSRange)_range{
    if (0 == _range.location
        && 0 == _range.length)
        return YES;
    
    NSError *error             = NULL;
    NSString *regexString      = [NSString stringWithFormat:@"^.{%ld,%ld}$", (unsigned long)_range.location, (unsigned long)_range.length];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:0 error:&error];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    return numberOfMatches == 1;
}

- (BOOL)checkShortURL{
    if (nil == self)
    {
        return NO;
    }
    
    NSError *error             = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^((https?)://)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$" options:NSRegularExpressionCaseInsensitive error:&error];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    return numberOfMatches == 1;
}

- (BOOL)checkURL{
    if (nil == self)
    {
        return NO;
    }
    
    NSError *error             = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^((https?)://)[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$" options:NSRegularExpressionCaseInsensitive error:&error];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    return numberOfMatches == 1;
}

- (BOOL)checkAlphanumeric{
    if (nil == self)
        return NO;
    
    NSError *error             = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[a-zA-Z0-9]" options:0 error:&error];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    return numberOfMatches == self.length;
}

-(BOOL)checkNumeric{
    BOOL isValid = NO;
    NSCharacterSet *alphaNumbersSet = [NSCharacterSet decimalDigitCharacterSet];
    NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:self];
    isValid = [alphaNumbersSet isSupersetOfSet:stringSet];
    return isValid;
}

- (UIColor *)getColor
{
    NSString *hexColor = self;
    unsigned int red,green,blue;
    NSRange range;
    
    range = [hexColor rangeOfString:@"#"];
    if (range.length > 0) {
        hexColor = [hexColor substringFromIndex:1];
    }
    
    if (hexColor.length != 6)
        return [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
    
    range.length = 2;
    
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&red];
    
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&green];
    
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&blue];
    
    return [UIColor colorWithRed:(float)(red/255.0f)green:(float)(green / 255.0f) blue:(float)(blue / 255.0f)alpha:1.0f];
}

+ (NSString *)convertSecondsToTimeString:(CGFloat)seconds
{
    int hour, minute, second;
    hour = seconds / 3600;
    int i = fmod(seconds, 3600);
    minute = i / 60;
    second = fmod(i, 60);
    NSString *hourString, *minuteString, *secondString;
    if (hour < 10) {
        hourString = [NSString stringWithFormat:@"0%d", hour];
    } else {
        hourString = [NSString stringWithFormat:@"%d", hour];
    }
    if (minute < 10) {
        minuteString = [NSString stringWithFormat:@"0%d",minute];
    } else {
        minuteString = [NSString stringWithFormat:@"%d", minute];
    }
    if (second < 10) {
        secondString = [NSString stringWithFormat:@"0%d", second];
    } else {
        secondString = [NSString stringWithFormat:@"%d", second];
    }
    NSString *timeString;
    if (hour == 0) {
        timeString = [NSString stringWithFormat:@"%@:%@", minuteString, secondString];
    } else {
        timeString = [NSString stringWithFormat:@"%@:%@:%@", hourString, minuteString, secondString];
    }
    return timeString;
}

- (NSString *)getStrippedStringWithLength:(NSUInteger)length{
    NSMutableString *result = [NSMutableString stringWithString:self];
    if (self.length > length) {
        [result replaceCharactersInRange:NSMakeRange(length, self.length - length) withString:@"..."];
    }
    return result;
}


@end
